package business.global.pk.ddz;

import cenum.RoomTypeEnum;
import jsproto.c2s.cclass.pk.Victory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DDZGameResult {
    public DDZRoom room; //房间

    public DDZGameResult(DDZRoom room) {
        this.room = room;
    }

    /**
     * 计算分数
     */
    public int calPoint() {
        DDZRoomSet set = (DDZRoomSet) this.room.getCurSet();
        int winPos = set.winPos;
        if (winPos == -1) {
            return -1;
        }
        boolean isLandownerWinner = set.winPos==set.landowner;
        calRobClose(set);
        int fengDing = room.getFengDing()>0?room.getFengDing():Integer.MAX_VALUE;
        DDZRoomPos pos = (DDZRoomPos) this.room.getRoomPosMgr().getPosByPosID(set.landowner);
        int landOwerDouble = this.room.getBaseMark() * set.roomDouble * set.getDoubleByPos(pos.getPosID())  * set.getHogScore();
        Victory victory = new Victory(-1, -1);
        for (int i = 0; i < this.room.getPlayerNum(); i++) {
            if (i == set.landowner) {
                continue;
            }
            int point =  set.getDoubleByPos(i) * landOwerDouble ;
            if (isLandownerWinner) {//地主赢
                set.pointList.set(i,  -point);
            } else {
                set.pointList.set(i,  point);
            }
            boolean isVictory = -1 != victory.getPos() && Math.abs( victory.getNum()) >  Math.abs(point);
            if (-1 == victory.getPos() || isVictory) {
                victory.setPos(i);
                victory.setNum(point);
            }
        }
        if (isLandownerWinner) {
            if (fengDing >= victory.getNum() * 2) {
                setPointByFengDingFenShu(pos.getPosID(), fengDing, victory);
            } else {
                setPointByFengDingFenShu(pos.getPosID(), fengDing/2);
            }
        } else {
            if (fengDing <= victory.getNum() * 2) {
                setPointByFengDingFenShu(pos.getPosID(), fengDing/2);
            } else {
                setPointByFengDingFenShu(pos.getPosID(), fengDing, victory);
            }
        }
        return winPos;
    }

    public void  setPointByFengDingFenShu(int landlordPos, int fengDingFenShu, Victory victory) {
        DDZRoomSet set = (DDZRoomSet) this.room.getCurSet();
        set.pointList.set(landlordPos,  0);
        boolean isLandownerWinner = set.winPos==set.landowner;
        //是否需要算分
        List<Boolean> needCalPoint = new ArrayList<>(Collections.nCopies(this.room.getPlayerNum(), true));
        if (set.room.isOnlyWinRightNowPoint() && RoomTypeEnum.UNION.equals(set.room.getRoomTypeEnum())) {
            for (int i = 0; i < this.room.getPlayerNum(); i++) {
                DDZRoomPos posByPosID = (DDZRoomPos) this.room.getRoomPosMgr().getPosByPosID(i);
                if(posByPosID.getGameBeginSportsPoint()<=0){
                    needCalPoint.set(i,false);
                }else{
                    needCalPoint.set(i,true);
                }
            }
        }
        for (int i = 0; i < this.room.getPlayerNum(); i++) {
            if (landlordPos == i) {
                continue;
            }
            if (isLandownerWinner) {
                if(needCalPoint.get(landlordPos)){
                    if (victory.getPos() == i) {
                        set.pointList.set(i, - victory.getNum());
                        set.pointList.set(landlordPos, set.pointList.get(landlordPos)+ victory.getNum());
                    } else {
                        int losePoint = Math.abs( set.pointList.get(i));
                        losePoint = fengDingFenShu - victory.getNum() > losePoint ? losePoint : fengDingFenShu - victory.getNum();
                        set.pointList.set(i, -losePoint);
                        set.pointList.set(landlordPos, set.pointList.get(landlordPos)+ losePoint);
                    }
                }else{
                    //地主不能赢分，所以其他人都不需要输
                    set.pointList.set(i,0);
                }
            } else {
                if(needCalPoint.get(i)){
                    if (victory.getPos() == i) {
                        set.pointList.set(i,  victory.getNum());
                        set.pointList.set(landlordPos, set.pointList.get(landlordPos)- victory.getNum());
                    } else {
                        int losePoint = Math.abs(set.pointList.get(i));
                        losePoint = fengDingFenShu - victory.getNum() > losePoint ? losePoint : fengDingFenShu - victory.getNum();
                        set.pointList.set(i, losePoint);
                        set.pointList.set(landlordPos, set.pointList.get(landlordPos)- losePoint);
                    }
                }else{
                    //农民不能赢分，所以地主不需要输，农民不需要赢
                    set.pointList.set(i,  0);
                }

            }
        }
    }

    /**
     * 根据封顶分数设置分数
     * */
    public void  setPointByFengDingFenShu(int landlordPos,int point) {
        DDZRoomSet set = (DDZRoomSet) this.room.getCurSet();
        boolean isLandownerWinner = set.winPos==set.landowner;
        set.pointList.set(landlordPos, 0);
        for (int i = 0; i < this.room.getPlayerNum(); i++) {
            if (landlordPos == i) {
                continue;
            }
            if (isLandownerWinner) {
                set.pointList.set(i,  -point);
                set.pointList.set(landlordPos, set.pointList.get(landlordPos)+ point);
            } else {
                set.pointList.set(i, point);
                set.pointList.set(landlordPos, set.pointList.get(landlordPos) - point);
            }
        }
    }

    /**
     * 计算春天和反春天
     * @param set
     */
    private void calRobClose(DDZRoomSet set) {
        boolean isLandownerWin = set.winPos==set.landowner;
        int springNum = 0;
        for (int i = 0; i < this.room.getPlayerNum(); i++) {
            DDZRoomPos pos = (DDZRoomPos) this.room.getRoomPosMgr().getPosByPosID(i);
            if (null == pos) {
                continue;
            }
            if (pos.getPosID() == set.landowner) {
                if (!isLandownerWin) {
                    //反春天，是地主春天农民
                    if (pos.getPublicCardList().size() <= 1) {
                        set.robCloseList.set(pos.getPosID(), -1);
                    }
                }
            } else {
                //春天，是农民春天地主
                if (isLandownerWin && pos.getPublicCardList().size() <= 0) {
                    set.robCloseList.set(pos.getPosID(), 1);
                    springNum++;
                }
            }
        }
        if(isLandownerWin && springNum!=this.room.getPlayerNum() - 1){//除了地主出牌，其他人没过出牌
            for (int i = 0; i < this.room.getPlayerNum(); i++) {
                if (i == set.landowner) {
                    continue;
                }
                set.robCloseList.set(i, 0);
            }
        }
    }
}
